﻿Imports SS.ApplicationBlock.Security

Public Class Officer

	Public Shared Function IsActive(ByVal userId As String) As Boolean
		Return DL.Officer.IsActive(userId)
	End Function

	Public Shared Sub AcceptAgreement(ByVal userId As String)
		DL.Officer.UpdateActiveDate(userId)
	End Sub

	Public Shared Function LoadSponser_Staff(ByVal appId As String) As Entity.OfficerInfoItem
		Dim ls As List(Of Entity.OfficerInfoItem) = DL.Officer.LoadSponser(appId, String.Empty)

		If ls IsNot Nothing AndAlso ls.Count > 0 Then
			Return ls(0)
		Else
			Return Nothing
		End If
	End Function

	Public Shared Function LoadSponser_Member(ByVal appId As String, ByVal officer_line_id As String) As Entity.OfficerInfoItem
		Dim ls As List(Of Entity.OfficerInfoItem) = DL.Officer.LoadSponser(appId, officer_line_id)

		If ls IsNot Nothing AndAlso ls.Count > 0 Then
			Return ls(0)
		Else
			Return Nothing
		End If
	End Function

	Public Shared Function LoadSender_Staff(ByVal appId As String) As Entity.OfficerInfoItem
		Dim ls As List(Of Entity.OfficerInfoItem) = DL.Officer.LoadSender(appId, String.Empty)

		If ls IsNot Nothing AndAlso ls.Count > 0 Then
			Return ls(0)
		Else
			Return Nothing
		End If
	End Function

	Public Shared Function LoadSender_Member(ByVal appId As String, ByVal sponsorId As String) As Entity.OfficerInfoItem
		Dim ls As List(Of Entity.OfficerInfoItem) = DL.Officer.LoadSender(appId, sponsorId)

		If ls IsNot Nothing AndAlso ls.Count > 0 Then
			Return ls(0)
		Else
			Return Nothing
		End If
	End Function

	Public Shared Function CheckDupAppID(ByVal officerAppId As String) As Boolean
		Return Not DL.Officer.CheckDupAppID(officerAppId.Replace("-", "")) = 0
	End Function

	Public Shared Function CheckDupAppTel(ByVal appTel As String) As Boolean
		Return Not DL.Officer.CheckDupAppTel(appTel, String.Empty) = 0
	End Function

	Public Shared Function CheckDupCardNo(ByVal cardNo As String) As Boolean
		Return Not DL.Officer.CheckDupCardNo(cardNo, String.Empty) = 0
	End Function

	Public Shared Function CheckDupAppTel(ByVal appTel As String, ByVal ignorOfficerId As String) As Boolean
		Return Not DL.Officer.CheckDupAppTel(appTel, ignorOfficerId) = 0
	End Function

	Public Shared Function CheckDupCardNo(ByVal cardNo As String, ByVal ignorOfficerId As String) As Boolean
		Return Not DL.Officer.CheckDupCardNo(cardNo, ignorOfficerId) = 0
	End Function

	Public Shared Function LoadOfficerInputer(ByVal NameLastTh As String, ByVal AppId As String, ByVal MemberDateForm As String, ByVal MemberDateTo As String, ByVal inputerId As String) As DataTable
		Return DL.Officer.LoadOfficer(NameLastTh, String.Empty, MemberDateForm, MemberDateTo, inputerId, AppId, String.Empty, String.Empty)
	End Function

    Public Shared Function LoadOfficer(ByVal nameLastTh As String, ByVal nameLastEn As String,
   ByVal memberDateForm As String, ByVal memberDateTo As String,
   ByVal appId As String, ByVal appTel As String, ByVal cardNo As String) As DataTable

        Return DL.Officer.LoadOfficer(nameLastTh, nameLastEn, memberDateForm, memberDateTo, String.Empty, appId, appTel, cardNo)
    End Function

	Public Shared Function SearchSponser_Staff(ByVal keyword As String) As DataTable
		Return DL.Officer.SearchSponser(keyword, String.Empty)
	End Function

	Public Shared Function SearchSponser_Member(ByVal keyword As String, ByVal officerId As String) As DataTable
		Return DL.Officer.SearchSponser(keyword, officerId)
	End Function

	Public Shared Function SearchUpline(ByVal keyword As String, ByVal sponsorId As String) As DataTable
		Return DL.Officer.SearchUpline(keyword, sponsorId)
	End Function

	Public Shared Function SearchSender(ByVal keyword As String, ByVal sponserId As String) As List(Of Entity.OfficerInfoItem)
		Return DL.Officer.SearchSender(keyword, sponserId)
	End Function

	Public Shared Sub ChangePassword(ByVal appId As String, ByVal newPass As String, ByVal officerId As String)
		If String.IsNullOrEmpty(appId) Then
			Throw New Exception("ไม่พบรหัสนักธุรกิจ")
		End If

		If String.IsNullOrEmpty(newPass) Then
			Throw New Exception("ท่านไม่ได้ระบุรหัสผ่านใหม่")
		End If

		Dim updateInfo As New Entity.UserInfoItem
		updateInfo.user_password = BL.Authentication.getMd5Hash(newPass)
		updateInfo.upd_date = Now
		updateInfo.upd_user_id = "Forget Password"
		updateInfo.user_force_change = "N"
		updateInfo.user_change_date = Now
		updateInfo.user_reset_url = ""

		Try
			If Not DL.Officer.UpdateUserInfo(updateInfo, officerId) Then
				Throw New Exception("ไม่สามารถแก้ไขรหัสผ่านได้ กรุณาติดต่อเจ้าหน้าที่")
			End If
		Catch ex As Exception
			Throw ex
		Finally
			updateInfo = Nothing
		End Try

	End Sub

	Public Shared Sub ChangePassword(ByVal officerId As String, ByVal oldPass As String, ByVal newPass As String, ByVal updateuserid As String)

		If String.IsNullOrEmpty(officerId) Then
			Throw New Exception("ไม่พบรหัสนักธุรกิจ")
		End If

		If String.IsNullOrEmpty(oldPass) Then
			Throw New Exception("ท่านไม่ได้ระบุรหัสผ่านเดิม")
		End If

		If String.IsNullOrEmpty(newPass) Then
			Throw New Exception("ท่านไม่ได้ระบุรหัสผ่านใหม่")
		End If

		Dim entUserInfo As New Entity.UserInfoItem
		Dim entOldInfo As Entity.UserInfoItem = DL.Officer.GetUserInfoByOfficerId(officerId)

		If entOldInfo Is Nothing Then
			entUserInfo = Nothing
			entOldInfo = Nothing
			Throw New Exception("ไม่ทราบดึงข้อมูลผู้ใช้งานจากฐานข้อมูลของระบบได้")
		End If

		If Not BL.Authentication.getMd5Hash(oldPass).Equals(entOldInfo.user_password) Then
			entUserInfo = Nothing
			entOldInfo = Nothing
			Throw New Exception("ท่านระบุรหัสผ่านเดิมไม่ถูกต้อง")
		End If

		entUserInfo.user_password = BL.Authentication.getMd5Hash(newPass)
		entUserInfo.upd_date = Now
		entUserInfo.upd_user_id = updateuserid
		entUserInfo.user_force_change = "N"
		entUserInfo.user_change_date = Now

		If Not DL.Officer.UpdateUserInfo(entUserInfo, officerId) Then
			Throw New Exception("ไม่สามารถแก้ไขรหัสผ่านได้ กรุณาติดต่อเจ้าหน้าที่")
		End If

		entUserInfo = Nothing
		entOldInfo = Nothing
	End Sub

	Public Shared Function UpdateStaff(ByVal info As Entity.OfficerInfoItem, ByVal userinfo As Entity.UserInfoItem) As Boolean
		Dim rslt As Boolean

		info.officer_app_id = info.officer_app_id.Replace("-", "")
		info.officer_downline_left = Nothing
		info.officer_downline_right = Nothing
		info.record_status = Nothing
		info.create_user_id = Nothing
		info.create_date = Nothing
		info.offcier_member_date = Nothing
        info.upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value


		Dim daoOfficer As New DL.Officer(True)

		Try

			Try
				rslt = daoOfficer.Update(info)
			Catch ex As Exception

				'Try
				'	daoOfficer.RollBack()
				'Catch
				'End Try

				If ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX1") Then
					Throw New Exception("ไม่สามารถใช้เลขที่ใบสมัคร / รหัสนักธุรกิจ " & info.officer_app_id & " ในการสมัครได้ เพราะพบเลขที่ใบสมัครนี้ในระบบแล้ว")
				ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX2") Then
					Throw New Exception("ไม่สามารถใช้หมายเลขโทรศัพท์ " & info.offcier_app_tel & " ในการสมัครได้ เพราะพบหมายเลขโทรศัพท์นี้ในระบบแล้ว")
				ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX3") Then
					Throw New Exception("พบหมายเลขบัตรประชาชน " & info.officer_card_no & " นี้ในการสมัครเป็นนักธุรกิจแล้ว")
				ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX4") Then
					Throw New Exception("ไม่สามารถใช้อัพไลน์เลขที่ " & info.officer_upline_appid & " ตำแหน่ง " & info.officer_upline_side & " ในการสมัครได้")
				Else
					Throw ex
				End If

			End Try

			If rslt Then
				If userinfo IsNot Nothing Then
					With userinfo
						.user_password = BL.Authentication.getMd5Hash(.user_password)
						.user_force_change = "Y"
                        .upd_user_id = info.officer_id
                        .upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
					End With
					rslt = daoOfficer.UpdateUserInfoHaveTransaction(userinfo, info.officer_id)
				End If
			End If

			If rslt Then
				daoOfficer.Commit()
				Return True
			Else
				daoOfficer.RollBack()
				Return False
			End If
		Catch ex As Exception
			Try
				daoOfficer.RollBack()
			Catch
			End Try
			Throw ex
		End Try

	End Function

	Public Shared Function AddStaff(ByVal info As Entity.OfficerInfoItem) As Boolean
		Dim rslt As Boolean

		info.officer_id = DL.Officer.NewId
		info.officer_type = "O"
		info.record_status = "A"
        'info.officer_card_type = "PID"
		info.officer_status = "NN"
		info.officer_app_id = info.officer_app_id.Replace("-", "")
        info.create_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
        info.upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value

		Dim daoOfficer As New DL.Officer(True)
		Try
			rslt = daoOfficer.Insert(info)
		Catch ex As Exception

			Try
				daoOfficer.RollBack()
			Catch
			End Try


			If ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX1") Then
				Throw New Exception("ไม่สามารถใช้เลขที่เข้าใช้งาน " & info.officer_app_id & " ในการสมัครได้")
			ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX2") Then
				Throw New Exception("ไม่สามารถใช้หมายเลขโทรศัพท์ในการทำธรุกิจหมายเลข " & info.offcier_app_tel & " ในการสมัครได้")
			ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX3") Then
				Throw New Exception("ไม่สามารถใช้อัพไลน์เลขที่ " & info.officer_upline_appid & " ตำแหน่ง " & info.officer_upline_side & " ในการสมัครได้")
			Else
				Throw ex
			End If

		End Try

		If rslt Then
			Dim userinfo As New Entity.UserInfoItem
			With userinfo
				.user_id = info.officer_app_id
				.user_password = BL.Authentication.getMd5Hash(info.officer_card_no.Substring(9, 4))
				.user_force_change = "Y"
				.record_status = "A"
                .create_user_id = info.officer_id
                .create_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
                .upd_user_id = info.officer_id
                .upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
				.offcier_id = info.officer_id
			End With
			rslt = daoOfficer.InsertUserInfo(userinfo)
		End If

		If rslt Then
			Dim OfficerRoll As New Entity.UserRowItem
			With OfficerRoll
				.create_user_id = info.create_user_id
                .create_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
                .user_id = info.officer_app_id
                .upd_user_id = info.officer_id
                .upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
				.role_id = "2"
			End With
			Dim dlUserRoll As New DL.Authentication(daoOfficer.CurrentConnection)
			rslt = dlUserRoll.InsertUserRole(OfficerRoll)
		End If

		If rslt Then
			daoOfficer.Commit()
			Return True
		Else
			daoOfficer.RollBack()
			Return False
		End If

	End Function

	Public Shared Function AddOfficer(ByVal info As Entity.OfficerInfoItem) As Boolean
		Dim rslt As Boolean

		info.officer_id = DL.Officer.NewId
		info.officer_type = "S"
		info.record_status = "A"
        'info.officer_card_type = "PID"
		info.officer_status = "WT"
		info.officer_app_id = info.officer_app_id.Replace("-", "")
		info.create_date = Now
		info.offcier_member_date = Now
		info.offcier_expird_date = Now.AddYears(1)
		info.officer_inputer = info.create_user_id
		info.upd_date = Now
		info.upd_user_id = info.create_user_id

		If Not String.IsNullOrEmpty(info.officer_upline) Then
			info.officer_upline_side = info.officer_upline_emptyside.ToString
		End If

		Dim daoOfficer As New DL.Officer(True)
		Try
			rslt = daoOfficer.Insert(info)
		Catch ex As Exception

			Try
				daoOfficer.RollBack()
			Catch
			End Try


			If ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX1") Then
				Throw New Exception("ไม่สามารถใช้เลขที่ใบสมัคร / รหัสนักธุรกิจ " & info.officer_app_id & " ในการสมัครได้ เพราะพบเลขที่ใบสมัครนี้ในระบบแล้ว")
			ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX2") Then
				Throw New Exception("ไม่สามารถใช้หมายเลขโทรศัพท์ " & info.offcier_app_tel & " ในการสมัครได้ เพราะพบหมายเลขโทรศัพท์นี้ในระบบแล้ว")
			ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX3") Then
				Throw New Exception("พบหมายเลขบัตรประชาชน " & info.officer_card_no & " นี้ในการสมัครเป็นนักธุรกิจแล้ว")
			ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX4") Then
				Throw New Exception("ไม่สามารถใช้อัพไลน์เลขที่ " & info.officer_upline_appid & " ตำแหน่ง " & info.officer_upline_side & " ในการสมัครได้")
			Else
				Throw ex
			End If

		End Try


		If rslt Then
			If Not String.IsNullOrEmpty(info.officer_upline) Then
				rslt = daoOfficer.UpdateDownLine(info.officer_upline, info.officer_id, info.officer_upline_emptyside.ToString)
			End If
		End If

		If rslt Then
			If Not String.IsNullOrEmpty(info.officer_sponsor) Then
				Dim log As New Entity.MainOfficerDownlineItem
				log.sponsor_id = info.officer_sponsor
				log.officer_id = info.officer_id
				log.create_user_id = info.create_user_id
				rslt = daoOfficer.InsertLogDownLine(log)
			End If
		End If

		If rslt Then
			Dim userinfo As New Entity.UserInfoItem
			With userinfo
				.user_id = info.officer_app_id
				.user_password = BL.Authentication.getMd5Hash(info.officer_card_no.Substring(9, 4))
				.user_force_change = "Y"
				.record_status = "A"
				.create_user_id = info.create_user_id
				.create_date = Now
				.offcier_id = info.officer_id
			End With
			rslt = daoOfficer.InsertUserInfo(userinfo)
		End If

		If rslt Then
			Dim OfficerRoll As New Entity.UserRowItem
			With OfficerRoll
				.create_user_id = info.create_user_id
				.create_date = Now
				.user_id = info.officer_app_id
				.role_id = "1"
			End With
			Dim dlUserRoll As New DL.Authentication(daoOfficer.CurrentConnection)
			rslt = dlUserRoll.InsertUserRole(OfficerRoll)
		End If

		If rslt Then
			daoOfficer.Commit()
			Return True
		Else
			daoOfficer.RollBack()
			Return False
		End If

	End Function

	Public Shared Function UpdateOfficer(ByVal info As Entity.OfficerInfoItem) As Boolean
		Dim daoOfficer As New DL.Officer(False)
		daoOfficer.Update(info)
		Return True
	End Function

	Public Shared Function UpdateOfficer(ByVal info As Entity.OfficerInfoItem, ByVal userinfo As Entity.UserInfoItem) As Boolean
		Dim rslt As Boolean

		info.officer_app_id = info.officer_app_id.Replace("-", "")
		info.officer_downline_left = Nothing
		info.officer_downline_right = Nothing
		info.record_status = Nothing
		info.create_user_id = Nothing
		info.create_date = Nothing
		info.offcier_member_date = Nothing

		If Not String.IsNullOrEmpty(info.officer_upline) Then
			info.officer_upline_side = info.officer_upline_emptyside.ToString
		End If

		Dim daoOfficer As New DL.Officer(True)

		Try

			'phase 2
			'add history log 
			rslt = daoOfficer.AddHistoryLog(info.officer_id)
			If rslt = False Then
				Throw New Exception("ไม่สามารถบันทึก history log ได้")
			End If

			Try

				If Not String.IsNullOrEmpty(info.officer_status) Then

					If info.officer_status.Equals("RENEW") Then
						If Not info.offcier_expird_date.HasValue Then
                            info.offcier_expird_date = DateTime.Today.AddYears(-543)
						End If
						info.offcier_expird_date = info.offcier_expird_date.Value.AddDays(365)
						info.officer_status = "WN"

						Dim renewInfo As New Entity.RememerInfoItem
						renewInfo.officer_id = info.officer_id
						renewInfo.create_user_id = info.upd_user_id
						renewInfo.upd_user_id = info.upd_user_id

						Try
							rslt = daoOfficer.RenewOfficer(renewInfo)
						Catch ex As Exception
							Throw New Exception("ไม่สามารถต่ออายุนักธุรกิจได้", ex)
						End Try
						If rslt = False Then
							Throw New Exception("ไม่สามารถต่ออายุนักธุรกิจได้")
						End If

					ElseIf info.officer_status.Equals("WN") Then
						If DL.Officer.isPaidInMonth(info.officer_id, DateTime.Today.ToString("yyyyMM", New Globalization.CultureInfo("en-GB"))) Then
							info.officer_status = "NM"
						Else
							info.officer_status = "WN"
						End If
					End If

				End If

				rslt = daoOfficer.Update(info)

				If rslt = False Then
					Throw New Exception("ไม่สามารถแก้ไขข้อมูลนักธุรกิจได้")
				End If
			Catch ex As Exception
				If ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX1") Then
					Throw New Exception("ไม่สามารถใช้เลขที่ใบสมัคร / รหัสนักธุรกิจ " & info.officer_app_id & " ในการสมัครได้")
				ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX2") Then
					Throw New Exception("ไม่สามารถใช้หมายเลขโทรศัพท์ในการทำธรุกิจหมายเลข " & info.offcier_app_tel & " ในการสมัครได้")
				ElseIf ex.Message.Contains("RNWM_MAIN_OFFICER_INFO_IX3") Then
					Throw New Exception("ไม่สามารถใช้อัพไลน์เลขที่ " & info.officer_upline_appid & " ตำแหน่ง " & info.officer_upline_side & " ในการสมัครได้")
				Else
					Throw New Exception("ไม่สามารถต่ออายุนักธุรกิจได้", ex)
				End If
			End Try

			If rslt Then
				'ล้าง downline R\L 
				rslt = daoOfficer.ClearDownline(info.officer_id)
			End If
			If rslt = False Then
				Throw New Exception("ไม่สามารถล้างข้อมูลดาว์นไลน์ได้")
			End If

			If rslt Then
				If Not String.IsNullOrEmpty(info.officer_upline) Then
					rslt = daoOfficer.UpdateDownLine(info.officer_upline, info.officer_id, info.officer_upline_emptyside.ToString)
					If rslt = False Then
						Throw New Exception("ไม่สามารถแก้ไขข้อมูลดาว์นไลน์ได้")
					End If
				End If
			End If

			If rslt Then
				If Not String.IsNullOrEmpty(info.officer_sponsor) Then
					Dim log As New Entity.MainOfficerDownlineItem
					If DL.Officer.CheckDowlineLog(info.officer_id) Then
						log.sponsor_id = info.officer_sponsor
						log.officer_id = info.officer_id
						log.upd_user_id = info.upd_user_id
						rslt = daoOfficer.UpdateLogDownLine(log)
						If rslt = False Then
							Throw New Exception("ไม่สามารถแก้ไขข้อมูล Log ดาว์นไลน์ได้")
						End If
					Else
						log.sponsor_id = info.officer_sponsor
						log.officer_id = info.officer_id
						log.create_user_id = info.upd_user_id
						rslt = daoOfficer.InsertLogDownLine(log)
						If rslt = False Then
							Throw New Exception("ไม่สามารถเพิ่มข้อมูล Log ดาว์นไลน์ได้")
						End If
					End If

				Else
					rslt = daoOfficer.DeleteLogDownLine(info.officer_id)
					If rslt = False Then
						Throw New Exception("ไม่สามารถลบข้อมูลดาว์นไลน์ได้")
					End If
				End If
			End If

			If rslt Then
				If userinfo IsNot Nothing Then
					With userinfo
						.user_password = BL.Authentication.getMd5Hash(.user_password)
						.user_force_change = "Y"
						.upd_user_id = info.upd_user_id
						.upd_date = Now
					End With
					rslt = daoOfficer.UpdateUserInfoHaveTransaction(userinfo, info.officer_id)
					If rslt = False Then
						Throw New Exception("ไม่สามารถเปลี่ยนรหัสผ่านได้")
					End If
				End If
			End If

			If rslt Then
				daoOfficer.Commit()
				Return True
			Else
				daoOfficer.RollBack()
				Return False
			End If
		Catch ex As Exception
			Try
				daoOfficer.RollBack()
			Catch
			End Try
			Throw ex
		End Try

	End Function

	Public Shared Function Authen(ByVal userid As String, ByVal pwd As String) As Entity.UserInfoItem

		Dim userinfo As Entity.UserInfoItem
		userinfo = DL.Officer.GetUserInfo(userid)

		If userinfo IsNot Nothing Then

			If BL.Authentication.getMd5Hash(pwd) = userinfo.user_password Then

				Return userinfo
			Else
				Throw New Exception("ไม่สามารถเข้าสู่ระบบได้")
			End If
		Else
			Throw New Exception("ไม่สามารถเข้าสู่ระบบได้")
		End If

	End Function

	Public Shared Function ForgatPassword(ByVal appId As String, ByVal firstName As String, _
	 ByVal lastName As String, ByVal cardNo As String, ByVal appTel As String) As String

		If String.IsNullOrEmpty(appId) Then
			Throw New Exception("กรุณาระบุรหัสนักธุรกิจ")
		End If

		If String.IsNullOrEmpty(firstName) Then
			Throw New Exception("กรุณาระบุชื่อ")
		End If

		If String.IsNullOrEmpty(lastName) Then
			Throw New Exception("กรุณาระบุนามสกุล")
		End If

		If String.IsNullOrEmpty(cardNo) Then
			Throw New Exception("กรุณาระบุบัตรประชาชน")
		End If

		If String.IsNullOrEmpty(appTel) Then
			Throw New Exception("กรุณาระบุหมายเลขโทรศัพท์ในการทำธุรกิจ")
		End If

		Dim strEmail As String = DL.Officer.LoadResetEmail(appId, firstName, lastName, cardNo, appTel)
		If String.IsNullOrEmpty(strEmail) Then
			Throw New Exception("ไม่พบข้อมูลที่ใช้ในการเปลี่ยนรหัสรหัสผ่าน กรุณาตรวจสอบว่าท่านได้ทำการระบุข้อมูลถูกต้องหรือไม่")
		End If

		Dim entUserInfo As Entity.UserInfoItem = DL.Officer.GetUserInfo(appId)
		If entUserInfo Is Nothing Then
			Throw New Exception("ไม่สามารถดึงข้อมูลเพื่อทำการแก้ไขรหัสผ่านได้")
		End If

		Dim rand As New Random
		Dim strKey As String = rand.Next.ToString()

		Dim entUpdateUrl As New Entity.UserInfoItem
		entUpdateUrl.user_reset_url = strKey
		entUpdateUrl.user_force_change = "N"
		If Not DL.Officer.UpdateUserInfo(entUpdateUrl, entUserInfo.offcier_id) Then
			Throw New Exception("ไม่สามารถกำหนดค่าเพื่อทำการของเปลี่ยนรหัสผ่านได้")
		End If

		Dim sb As New Text.StringBuilder
		Dim key As String = DL.SystemConfig.LoadValue("DES_KEY")

		sb.AppendLine("คุณได้ส่งคำขอเพื่อที่จะเปลี่ยนรหัสรหัสผ่านของคุณ กรุณากดลิ้งค์ต่อไปนี้ เพื่อทำการกำหนดรหัสผ่านใหม่ของท่าน:")
		sb.AppendLine(DL.SystemConfig.LoadValue("URL") & "ResetPassword.aspx?seckey=" & strKey & "&key=" & appId)

		Try
			Common.Utility.SendMail(DL.SystemConfig.LoadValue("ADMIN_MAIL"), strEmail, sb.ToString, "real network : You requested a new password")
			Return strEmail
		Catch ex As Exception
			Common.LogError.AddLogException(ex)
			Throw New Exception("ไม่สามารถส่งเมล์เพื่อทำการขอเปลี่ยนรหัสรหัสผ่านได้")
		End Try
	End Function

	Public Shared Function DecryptAppId(ByVal key As String) As String
		Return DES.DecryptString(key, DL.SystemConfig.LoadValue("DES_KEY"))
	End Function

	Public Shared Function CheckKeyForResetPassword(ByVal resetURL As String) As String

		Return DL.Officer.LoadForReset(resetURL)

	End Function

	Public Shared Function LoadProfile(ByVal officerID As String) As Entity.OfficerInfoItem

		Return DL.Officer.LoadProfile(officerID)

	End Function

	Public Shared Function LoadSummaryInfo(ByVal officerID As String) As Entity.SumInfoItem
		Dim rslt As Entity.SumInfoItem = DL.Officer.LoadSummaryInfo(officerID)
		If rslt IsNot Nothing Then
			rslt.officer_status = Common.Utility.ConvertStatus(rslt.officer_status)
			Try
				rslt.countchildlv1 = BL.Report.CountDownlineLv1(rslt.officer_id)
				rslt.countchildlv2 = BL.Report.CountDownlineLv2(rslt.officer_id)
				rslt.countchildlv3 = BL.Report.CountDownlineLv3(rslt.officer_id)
				rslt.countchildlv4 = BL.Report.CountDownlineLv4(rslt.officer_id)
				rslt.countchildlv5 = BL.Report.CountDownlineLv5(rslt.officer_id)
			Catch 
			End Try
		End If
		Return rslt
	End Function

    Public Shared Function GetOfficerIncome(ByVal month As String, ByVal year As String, ByVal telSuccess As String, ByVal officerStatus As String, ByVal officerAppId As String, ByVal paidStatus As String) As Data.DataTable
        Return DL.Officer.GetOfficerIncome(month, year, telSuccess, officerStatus, officerAppId, paidStatus)
    End Function

	Public Shared Function GetUpline(ByVal officerId As String) As String
		Return DL.Officer.GetUpline(officerId)
	End Function

	Public Shared Function GetOfficerId(ByVal officerAppId As String) As String
		Return DL.Officer.GetOfficerId(officerAppId)
    End Function

    Public Shared Function UpdateOfficerIncome(ByVal ofcIncList As List(Of Entity.OfficerIncome)) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim daoOfficer As New DL.Officer(True)

        Try
            For Each obj As Entity.OfficerIncome In ofcIncList
                rslt = daoOfficer.InsertOfficerIncomeHistory(obj.summary_id)
                If Not rslt Then Exit For
                rslt = daoOfficer.UpdateOfficerIncome(obj)
                If Not rslt Then Exit For
            Next

            If rslt Then
                daoOfficer.Commit()
                Return True
            Else
                daoOfficer.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                daoOfficer.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

    Public Shared Function GetOfficerInfo(ByVal officerId As String) As DataTable
        Return DL.Officer.GetOfficerInfo(officerId)
    End Function

#Region "Officer Payment"

    Public Shared Function GetOfficerPayment(ByVal officerId As String) As DataTable
        Return DL.Officer.GetOfficerPayment(officerId)
    End Function

    Public Shared Function InsertOfficerPayment(ByVal info As Entity.OfficerPayment) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.InsertOfficerPayment(info)
    End Function

    Public Shared Function UpdateOfficerPayment(ByVal info As Entity.OfficerPayment) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.UpdateOfficerPayment(info)
    End Function

#End Region

#Region "Officer Remember"

    Public Shared Function GetOfficerRemember(ByVal officerId As String) As DataTable
        Return DL.Officer.GetOfficerRemember(officerId)
    End Function

    Public Shared Function InsertOfficerRemember(ByVal info As Entity.OfficerRemember, ByVal officerId As String, ByVal upd_user_id As String) As Boolean
        Dim dt As DataTable = New DataTable
        dt = DL.Officer.GetOfficerExpiredDate(officerId)

        Dim expireddate As String = ""
        If Not IsDBNull(dt.Rows(0).Item("OFFCIER_EXPIRD_DATE")) Then
            Dim OFFCIER_EXPIRD_DATE As Date = dt.Rows(0).Item("OFFCIER_EXPIRD_DATE")
            expireddate = OFFCIER_EXPIRD_DATE.AddYears(-542).ToString("yyyyMMdd")
        Else
            expireddate = Now.AddYears(-542).ToString("yyyyMMdd")
        End If

        Dim rslt As Boolean
        rslt = True
        Dim daoOfficer As New DL.Officer(True)

        Try
            rslt = daoOfficer.InsertOfficerRemember(info)
            If rslt Then
                rslt = daoOfficer.UpdateOfficerExpiredDate(officerId, expireddate, upd_user_id)
            End If

            If rslt Then
                daoOfficer.Commit()
                Return True
            Else
                daoOfficer.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                daoOfficer.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

    Public Shared Function UpdateOfficerRemember(ByVal info As Entity.OfficerRemember, ByVal officerId As String, ByVal upd_user_id As String) As Boolean
        Dim dt As DataTable = New DataTable
        dt = DL.Officer.GetOfficerExpiredDate(officerId)

        Dim expireddate As String = ""
        If Not IsDBNull(dt.Rows(0).Item("OFFCIER_EXPIRD_DATE")) Then
            Dim OFFCIER_EXPIRD_DATE As Date = dt.Rows(0).Item("OFFCIER_EXPIRD_DATE")
            expireddate = OFFCIER_EXPIRD_DATE.AddYears(-544).ToString("yyyyMMdd")
        Else
            expireddate = Now.ToString("yyyyMMdd")
        End If

        Dim rslt As Boolean
        rslt = True
        Dim daoOfficer As New DL.Officer(True)

        Try
            rslt = daoOfficer.UpdateOfficerRemember(info)
            If rslt Then
                rslt = daoOfficer.UpdateOfficerExpiredDate(officerId, expireddate, upd_user_id)
            End If

            If rslt Then
                daoOfficer.Commit()
                Return True
            Else
                daoOfficer.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                daoOfficer.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

#End Region

#Region "Officer Attach"

    Public Shared Function GetOfficerAttach(ByVal officerId As String) As DataTable
        Return DL.Officer.GetOfficerAttach(officerId)
    End Function

    Public Shared Function GetOfficerAttachById(ByVal attachid As String) As DataTable
        Return DL.Officer.GetOfficerAttachById(attachid)
    End Function

    Public Shared Function InsertOfficerAttach(ByVal info As Entity.OfficerAttach) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.InsertOfficerAttach(info)
    End Function

    Public Shared Function UpdateOfficerAttachByPath(ByVal info As Entity.OfficerAttach) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.UpdateOfficerAttachByPath(info)
    End Function

    Public Shared Function UpdateOfficerAttach(ByVal info As Entity.OfficerAttach) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.UpdateOfficerAttach(info)
    End Function

    Public Shared Function DeleteOfficerAttach(ByVal AttachId As String) As Boolean
        Dim daoOfficer As New DL.Officer(False)
        Return daoOfficer.DeleteOfficerAttach(AttachId)
    End Function

#End Region

End Class
